﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IOEntity;
using IOBusiness;
using System.Data;
namespace Indochinaodyssey.Admin
{
    public partial class QLDanhMuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Quản lý danh mục";
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            mtvQLDanhMuc.ActiveViewIndex = 1;
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            clsBSDanhMuc d = new clsBSDanhMuc();
            clsDanhMuc dm = new clsDanhMuc();
            dm.TenDanhMuc = txtName.Text;
            dm.MoTa = txtMota.Text;
            dm.MyType = Convert.ToInt32(cbxType.SelectedValue);
            if (d.InsertDM(dm))
            {
                lblMes.Text = "<script>alert('Danh mục đã được tạo');</script>";
                mtvQLDanhMuc.ActiveViewIndex = 0;
                grvDanhMuc.DataBind();
            }
            else {
                lblMes.Text = "<script>alert('Lỗi, vui lòng kiểm tra dữ liệu !');</script>";
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            clsBSDanhMuc d = new clsBSDanhMuc();
            clsDanhMuc dm = new clsDanhMuc();
            dm.TenDanhMuc = txtNameEdit.Text;
            dm.MoTa = txtMoTaEdit.Text;
            //dm.MyType = Convert.ToInt32(cbxTypeEdit.SelectedValue);
            dm.Id = Convert.ToInt32(hfIDEdit.Value);
            if (d.EditDM(dm))
            {
                lblMes.Text = "<script>alert('Danh mục đã được sửa');</script>";
                mtvQLDanhMuc.ActiveViewIndex = 0;
                grvDanhMuc.DataBind();
            }
            else
            {
                lblMes.Text = "<script>alert('Lỗi, vui lòng kiểm tra dữ liệu !');</script>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            mtvQLDanhMuc.ActiveViewIndex = 0;
            lblMes.Text = "";
        }

        protected void btnSua_Click(object sender, EventArgs e)
        {
            
            LinkButton btnSua=(LinkButton)sender;
            HiddenField h =(HiddenField)btnSua.FindControl("hfID");
            hfIDEdit.Value = h.Value;
            clsBSDanhMuc d = new clsBSDanhMuc();
            string sql="Select * from tblDanhMuc where ID="+h.Value;
            DataTable table = d.GetDataTableSQL(sql, "DanhMuc");
            txtNameEdit.Text = table.Rows[0]["TenDanhMuc"].ToString();
            txtMoTaEdit.Text = table.Rows[0]["Mota"].ToString();
            //cbxTypeEdit.SelectedValue = table.Rows[0]["TypeID"].ToString();
            mtvQLDanhMuc.ActiveViewIndex = 2;
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            LinkButton lbt = (LinkButton)sender;
            HiddenField h = (HiddenField)lbt.FindControl("hfID");
            clsDanhMuc tl = new clsDanhMuc();
            tl.Id = Convert.ToInt32(h.Value);
            clsBSDanhMuc t = new clsBSDanhMuc();
            if (t.DelDM(tl))
            {
                lblMes.Text = "<script>alert('Danh mục đã được xóa');</script>";
                mtvQLDanhMuc.ActiveViewIndex = 0;
                grvDanhMuc.DataBind();
            }
            else
            {
                lblMes.Text = "<script>alert('Lỗi, không thể xóa !');</script>";
            }
        }
    }
}